package com.qxy.practice.collection.list;

import java.util.Arrays;

public class QuickSort {
    public static void quickSort(int [] arr ,int left,int right){
        if(left >= right ){
            return ;
        }
        int referIndex = partition(arr,left,right);
        quickSort(arr,referIndex+1,right);
        quickSort(arr,left,referIndex-1);
    }
    public static int partition(int [] arr ,int left,int right){
        int pivot =  arr[right];
        int i = left;
        for(int j = left ; j < right ;j++){
            if(arr[j] <= pivot){
                swap(arr,i,j);
                i++;
            }
        }
        swap(arr,i,right);
        return i;
    }

    public static void swap(int [] arr ,int left,int right){
        int tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
    }

    // 示例测试
    public static void main(String[] args) {
        int[] nums = {5, 2, 9, 1, 5, 6};
        quickSort(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums)); // 输出: [1, 2, 5, 5, 6, 9]
    }
}